package com.hzdaba.aiapi.controller;

import com.hzdaba.aiapi.domain.AiNumList;
import com.hzdaba.aiapi.dto.NumsImportDto;
import com.hzdaba.aiapi.service.AiNumListService;
import com.hzdaba.aiapi.vo.AiApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 接收号码相关请求
 *
 * @author 刘逸晖
 */
@RestController
@RequestMapping("/api/num")
public class AiNumListController {

    /**
     * 处理号码相关业务
     */
    @Autowired
    private AiNumListService aiNumListService;

    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(AiNumListController.class);

    /**
     * 导入号码时，一次最多可以导入多少条数据
     */
    @Value("${aiapi.ainumlist.import.size.max}")
    private int maxSizeOfImport;

    /**
     * 这是用于生成导入号码的测试数据的
     *
     * @return 测试数据
     */
    @GetMapping("/generateImportDate")
    public NumsImportDto toImport(HttpServletRequest request) {
        int count = ThreadLocalRandom.current().nextInt(1000);

        List<AiNumList> aiNumList = new ArrayList<>(count);

        AiNumList temp = null;

        String batchId = UUID.randomUUID().toString().substring(0, 32);//下发批次ID

        Long addTime = new Date().getTime();//添加时间

        for (int i = 0; i < count; i++) {
            temp = new AiNumList();

            temp.setPhone("130" + ThreadLocalRandom.current().nextInt(10000000, 99999999));
            temp.setActId(null);
            temp.setBatchId(batchId);
            temp.setAddTime(addTime);
            temp.setSourceIp(request.getRemoteAddr());

            aiNumList.add(temp);
        }


        NumsImportDto numsImportDto = new NumsImportDto();
        numsImportDto.setReqId(UUID.randomUUID().toString());
        numsImportDto.setList(aiNumList);
        return numsImportDto;
    }

    /**
     * 导入号码
     *
     * @param apiToken     token
     * @param numImportDto 号码信息
     * @param response
     * @return 成功与否
     */
    @PostMapping("/import")
    public AiApiResponse importNumbers(@RequestHeader("${aiapi.aicustomer.token.all.request.header.name}") String apiToken, @RequestBody NumsImportDto numsImportDto, HttpServletResponse response) {

        if (numsImportDto.getList().size() > this.maxSizeOfImport) {//号码数量太多，拒绝导入
            response.setStatus(403);
            return AiApiResponse.fail("导入数据必须<=" + this.maxSizeOfImport);
        }

        //导入号码
        return this.aiNumListService.importNums(numsImportDto);
    }

    /**
     * 用于测试回调请求的
     * 会被拦截器拦截
     *
     * @param aiApiResponse 回调结果
     * @return 回调响应
     */
    @PostMapping("/callback")
    public AiApiResponse callback(@RequestBody AiApiResponse aiApiResponse) {
        logger.info("收到回调请求：" + aiApiResponse.toString());
        return AiApiResponse.success("已收到回调请求");
    }


}
